Chart for WinRT > Chart Features > Data Binding > Data Binding in Code |
The steps required to create data bound charts are as follows:
The steps listed above are similar to the steps you've used to create other C1Charts, except for Steps 3 and 4.
To illustrate the difference, take a look at the code used to create a chart without bound data:
C# Unbound |
Copy Code
|
---|---|
// Get the data var data = GetSalesPerRegionData(); // Show regions along label axis _c1Chart.Data.ItemNames = ( from r in data select r.Region).ToArray(); // Add Revenue series var ds = new DataSeries(); ds.Label = "Revenue"; ds.ValuesSource = (from r in data select r.Revenue).ToArray(); _c1Chart.Data.Children.Add(ds); // Add Expense series ds = new DataSeries(); ds.Label = "Expense"; ds.ValuesSource = (from r in data select r.Expense).ToArray(); _c1Chart.Data.Children.Add(ds); // Add Profit series ds = new DataSeries(); ds.Label = "Profit"; ds.ValuesSource = (from r in data select r.Profit).ToArray(); _c1Chart.Data.Children.Add(ds); |
Here is the data-bound version of the code. The result is identical:
C# Bound |
Copy Code
|
---|---|
// Get the data var data = GetSalesPerRegionData(); chart.Data.ItemsSource = data; //Show regions along label axis chart.Data.ItemNameBinding = newBinding() { Path = newPropertyPath("Region") }; // Add data series foreach(stringseries in"Revenue,Expense,Profit".Split(',')) { vards = newDataSeries(); ds.Label = series; ds.ValueBinding = newBinding() { Path = newPropertyPath(series) }; chart.Data.Children.Add(ds); } |
The data-bound version of the code is even more compact than the original. The three series are created in a loop, taking advantage of the fact that the names of the properties we want to chart are the same as the names we want to use for each data series.